<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <a-date-picker 
                :disabled="formDisabled" 
                placeholder="请选择开始时间"
                valueFormat="YYYY-MM-DD"
                @change="handleChange"
                v-model="model.beginDate"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <a-date-picker
                :disabled="formDisabled"
                :disabled-date="disabledDate"
                :default-picker-value="this.model.beginDate"
                placeholder="请选择结束时间"
                v-model="model.endDate"
                valueFormat="YYYY-MM-DD"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工作单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
              <a-input :disabled=formDisabled v-model="model.workUnit" placeholder="请输入工作单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nature">
              <a-input :disabled=formDisabled v-model="model.nature" placeholder="请输入性质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="department">
              <a-input :disabled=formDisabled v-model="model.department" placeholder="请输入部门"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <a-input :disabled=formDisabled v-model="model.position" placeholder="请输入岗位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="证明人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="witness">
              <a-input :disabled=formDisabled v-model="model.witness" placeholder="请输入证明人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="证明人联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="witnessPhone">
              <a-input :disabled=formDisabled v-model="model.witnessPhone" placeholder="请输入证明人联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="薪资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salary">
              <!-- <j-date :disabled=formDisabled placeholder="请选择薪资" v-model="model.salary"  style="width: 100%" /> -->
              <a-input :disabled=formDisabled v-model="model.salary" placeholder="请输入薪资"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="离职原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveReason">
              <a-input :disabled=formDisabled v-model="model.leaveReason" placeholder="请输入离职原因"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import moment from 'moment';

  export default {
    name: 'ErpHrApplyWorkHistoryForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          beginDate: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
          endDate: [{ required: true, message: '请选择结束时间', trigger: 'change' }],
          workUnit: [{ required: true, message: '请输入工作单位', trigger: 'blur' }],
          nature: [{ required: true, message: '请输入性质', trigger: 'blur' }],
          department: [{ required: true, message: '请输入部门', trigger: 'blur' }],
          position: [{ required: true, message: '请输入岗位', trigger: 'blur' }],
          witness: [{ required: true, message: '请输入证明人', trigger: 'blur' }],
          witnessPhone: [{ required: true, pattern: /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/, message: '请输入正确的证明人联系电话', trigger: 'blur' }],
          salary: [{ required: true,pattern: /^[1-9]\d*$/, message: '请输入正确薪资', trigger: 'blur' }],
          leaveReason: [{ required: true, message: '请输入离职原因', trigger: 'blur' }],
        },
        url: {
          add: "/api/erp/hr/apply/erpHrApplyWorkHistory/add",
          edit: "/api/erp/hr/apply/erpHrApplyWorkHistory/edit",
          queryById: "/api/erp/hr/apply/erpHrApplyWorkHistory/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      moment,
      handleChange(value) {
        if (this.model.endDate) {
          if ((this.model.endDate).diff((this.model.beginDate), 'days') < 0) {
            this.model.endDate = this.model.beginDate
          }
        }
      },
      disabledDate(current) {
        // if(this.model.beginDate){
        //   return current > moment().endOf('day') || current < moment(this.model.beginDate).subtract('day')
        // } else {
        //   return current > moment().endOf('day')
        // }
        return current < moment(this.model.beginDate).subtract('day')
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            // that.confirmLoading = true;
            // let httpurl = '';
            // let method = '';
            // if(!this.model.id){
            //   httpurl+=this.url.add;
            //   method = 'post';
            // }else{
            //   httpurl+=this.url.edit;
            //    method = 'put';
            // }
            // httpAction(httpurl,this.model,method).then((res)=>{
            //   if(res.success){
            //     that.$message.success(res.message);
            //     that.$emit('ok');
            //   }else{
            //     that.$message.warning(res.message);
            //   }
            // }).finally(() => {
            //   that.confirmLoading = false;
            // })
            that.$emit('ok',that.model);
          }

        })
      },
    }
  }
</script>
